package com.example.villagedevelop.generator.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.villagedevelop.generator.entity.Case0;
import com.example.villagedevelop.generator.entity.CaseVO;
import com.example.villagedevelop.generator.entity.ImageCase;
import com.example.villagedevelop.generator.mapper.CaseMapper;
import com.example.villagedevelop.generator.mapper.ImageCaseMapper;
import com.example.villagedevelop.generator.service.CaseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.villagedevelop.generator.service.ImageCaseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author villagedevelop
 * @since 2021-11-13
 */
@Service
public class CaseServiceImpl extends ServiceImpl<CaseMapper, Case0> implements CaseService {
    @Autowired
    CaseMapper mapper;
    @Autowired
    ImageCaseService imageCaseService;
    @Autowired
    ImageCaseMapper imageCaseMapper;



    @Override
    public List<CaseVO> findCaseVOByPage(int current) {
        List<CaseVO> caseVOList = new ArrayList<>();
        Page<Case0> page = new Page<>(current,5);
        IPage<Case0> result = mapper.selectPage(page,null);
        List<Case0> list = result.getRecords();

        for (Case0 ca : list) {
            CaseVO caseVO = new CaseVO();
            BeanUtils.copyProperties(ca,caseVO);
            int cid = ca.getId();
            caseVO.setImages(imageCaseService.getImagesByCid(cid));//将图片集合封装在对象里
            caseVOList.add(caseVO);
        }
        return caseVOList;
    }

    @Override
    public void insertCaseVO(CaseVO caseVO) {
        Case0 ca = new Case0();
        BeanUtils.copyProperties(caseVO,ca);
        mapper.insert(ca);
        ImageCase image = new ImageCase();
        int cid = ca.getId();
        for (String url : caseVO.getImages()) {
            image.setCId(cid);
            image.setImageFile(url);
            imageCaseMapper.insert(image);
        }



    }
}
